package com.zixi.beverage_order.service.seller;

import com.zixi.beverage_order.mapper.OrderListMapper;
import com.zixi.beverage_order.mapper.OrderedProductMapper;
import com.zixi.beverage_order.mapper.ProductMapper;
import com.zixi.beverage_order.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OrderServiceImpl implements OrdersService {

    @Autowired
    OrderListMapper orderListMapper;

    @Autowired
    OrderedProductMapper orderedProductMapper;

    @Autowired
    ProductMapper productMapper;

    @Override
    public List<ReturnOderListInfo> getPickUpAllOrders() {
        Map<String, Object> map = new HashMap<>();
        map.put("address", null);
        List<OrderList> orderLists = orderListMapper.selectByMap(map);
        List<ReturnOrderList> returnOrderLists = new ArrayList<ReturnOrderList>();
        for (OrderList n : orderLists) {
            //每一个订单
            Map<String, Object> map1 = new HashMap<>();
            map1.put("order_list_id", n.getId());
            List<OrderedProduct> orderedProducts = orderedProductMapper.selectByMap(map1);
            List<ReturnOrderedProduct> returnOrderedProducts = new ArrayList<>();
            for (OrderedProduct m : orderedProducts) {
                //每一个产品
                returnOrderedProducts.add(new ReturnOrderedProduct(m.getId().toString(), m.getOrderListId().toString(), m.getProductId(), m.getTemperature(), m.getSweetness()));
            }
            ReturnOrderList returnOrderList = new ReturnOrderList(n.getId().toString(), n.getStarttime(), n.getFinishtime(), n.getRemark(), n.getCustomerId(), n.getAddress(), n.getPacked(), returnOrderedProducts);
            returnOrderLists.add(returnOrderList);
        }//转换
        List<ReturnOderListInfo> returnOderListInfoList = returnOrderList2ReturnOderListInfo(returnOrderLists);

        return returnOderListInfoList;//封装返回

    }

    @Override
    public List<ReturnOderListInfo> getDeliveryAllOrders() {
        Map<String, Object> map = new HashMap<>();
        List<OrderList> orderLists = orderListMapper.selectByMap(map);
        List<ReturnOrderList> returnOrderLists = new ArrayList<ReturnOrderList>();
        for (OrderList n : orderLists) {
            if (n.getAddress() == null)
                continue;
            Map<String, Object> map1 = new HashMap<>();
            map1.put("order_list_id", n.getId());
            List<OrderedProduct> orderedProducts = orderedProductMapper.selectByMap(map1);
            List<ReturnOrderedProduct> returnOrderedProducts = new ArrayList<>();
            for (OrderedProduct m : orderedProducts) {
                returnOrderedProducts.add(new ReturnOrderedProduct(m.getId().toString(), m.getOrderListId().toString(), m.getProductId(), m.getTemperature(), m.getSweetness()));
            }
            ReturnOrderList returnOrderList = new ReturnOrderList(n.getId().toString(), n.getStarttime(), n.getFinishtime(), n.getRemark(), n.getCustomerId(), n.getAddress(), n.getPacked(), returnOrderedProducts);
            returnOrderLists.add(returnOrderList);
        }//转换
        List<ReturnOderListInfo> returnOderListInfoList = returnOrderList2ReturnOderListInfo(returnOrderLists);
        return returnOderListInfoList;//封装返回
    }

    @Override
    public Object finishedOrder(Long finishedId) {
        OrderList orderList = orderListMapper.selectById(finishedId);
        orderList.setFinishtime(new Timestamp(System.currentTimeMillis()));
        orderListMapper.updateById(orderList);
        return null;
    }

    public List<ReturnOderListInfo> returnOrderList2ReturnOderListInfo(List<ReturnOrderList> returnOrderLists) {
        if (returnOrderLists.isEmpty())
            return null;
        List<ReturnOderListInfo> returnOderListInfoList = new ArrayList<>();
        for (ReturnOrderList n : returnOrderLists) {
            if (n.getStarttime()==null)
                continue;
            //每一个订单
            ReturnOderListInfo returnOderListInfo = new ReturnOderListInfo();
            //change ID
            returnOderListInfo.setId(n.getId());
            //change starttime
            returnOderListInfo.setStarttime(n.getStarttime().toString().substring(0, 10) + " " + n.getStarttime().toString().substring(11, 19));
            //change finishtime
            if (n.getFinishtime() != null)
                returnOderListInfo.setFinishtime(n.getFinishtime().toString().substring(0, 10) + " " + n.getFinishtime().toString().substring(11, 19));
            //change remark
            returnOderListInfo.setRemark(n.getRemark());
            //change customerId
            returnOderListInfo.setCustomerId(n.getCustomerId());
            //change address
            returnOderListInfo.setAddress(n.getAddress());
            //change packed
            if (n.getPacked()) {
                returnOderListInfo.setPacked("打包");
            } else {
                returnOderListInfo.setPacked("不打包");
            }
            //change orderedProducts
            int totalPrice = 0;
            List<ReturnOrderedProductInfo> returnOrderedProductInfoList = new ArrayList<>();
            for (ReturnOrderedProduct m : n.getOrderedProducts()) {
                //每一个产品
                ReturnOrderedProductInfo returnOrderedProductInfo = new ReturnOrderedProductInfo();
                //写入产品名
                returnOrderedProductInfo.setProductName(productMapper.selectById(m.getProductId()).getName());
                //写入温度
                //extra Hot=2; Hot=1; normal0; no ice=-1; less ice=-2; iced=-3
                //no sugar=0; 25% sugar=1; 50% sugar=2; 75% sugar=3; 100% sugar=4
                if (m.getTemperature() == 2) {
                    returnOrderedProductInfo.setTemperature("烫热");
                } else if (m.getTemperature() == 1) {
                    returnOrderedProductInfo.setTemperature("温热");
                } else if (m.getTemperature() == 0) {
                    returnOrderedProductInfo.setTemperature("常温");
                } else if (m.getTemperature() == -1) {
                    returnOrderedProductInfo.setTemperature("去冰");
                } else if (m.getTemperature() == -2) {
                    returnOrderedProductInfo.setTemperature("少冰");
                } else if (m.getTemperature() == -3) {
                    returnOrderedProductInfo.setTemperature("正常冰");
                }
                if (m.getSweetness() == 0) {
                    returnOrderedProductInfo.setSweetness("无糖");
                } else if (m.getSweetness() == 1) {
                    returnOrderedProductInfo.setSweetness("微糖");
                } else if (m.getSweetness() == 2) {
                    returnOrderedProductInfo.setSweetness("半糖");
                } else if (m.getSweetness() == 3) {
                    returnOrderedProductInfo.setSweetness("少糖");
                } else if (m.getSweetness() == 4) {
                    returnOrderedProductInfo.setSweetness("全糖");
                }
                returnOrderedProductInfoList.add(returnOrderedProductInfo);
                totalPrice += productMapper.selectById(m.getProductId()).getPrice();
            }
            returnOderListInfo.setTotalPrice(totalPrice);

//            List<String> stringList = new ArrayList<>();
//            for (ReturnOrderedProductInfo m : returnOrderedProductInfoList) {
//                String str = "产品名:" + m.getProductName() + " 甜度:" + m.getSweetness() + " 温度:" + m.getTemperature();
//                stringList.add(str);
//            }

//            returnOderListInfo.setOrderedProductList(stringList);
            returnOderListInfoList.add(returnOderListInfo);
        }
        return returnOderListInfoList;
    }

}
